package tech.yangzhijie.blog.admin.config.authentication;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

import javax.annotation.Resource;

/**
 * @Author: 探险家劫
 * @Date: 2021/11/18 21:05
 */
@Configuration
@EnableWebSecurity
// @EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    private final MyAccessDeniedHandler accessDeniedHandler;
    private final MyAuthenticationFailureHandler failureHandler;
    private final MyAuthenticationSuccessHandler successHandler;
    private final SecurityUserDetailsService userDetailsService;

    public SecurityConfig(MyAccessDeniedHandler accessDeniedHandler, MyAuthenticationFailureHandler failureHandler, MyAuthenticationSuccessHandler successHandler, SecurityUserDetailsService userDetailsService) {
        this.accessDeniedHandler = accessDeniedHandler;
        this.failureHandler = failureHandler;
        this.successHandler = successHandler;
        this.userDetailsService = userDetailsService;
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                //.exceptionHandling().authenticationEntryPoint(new MyAuthenticationEntryPoint()).and()
                .authorizeRequests()
                //.httpBasic().authenticationEntryPoint(authenticationEntryPoint)
                //.and()
                .anyRequest()
                .authenticated()// 其他 url 需要身份认证
                .and()
                .formLogin()  //开启登录
                .usernameParameter("username")
                .passwordParameter("password")
                .loginProcessingUrl("/login")
                .successHandler(successHandler) // 登录成功
                .failureHandler(failureHandler) // 登录失败
                .permitAll()
                .and()
                .logout()
                .logoutUrl("/logout")
                //.logoutSuccessHandler(logoutSuccessHandler)
                .permitAll();
        //.and()
        //.exceptionHandling().accessDeniedHandler(accessDeniedHandler);
    }

    public BCryptPasswordEncoder bCryptPasswordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
